package com.aeasycredit.uploaddatas;

import java.io.IOException;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.rainhy.core.exception.RainhyException;

public class HTTPUtils {
    
    /**
     * 
     * get 协议请求
     *
     * @param url
     * 
     * @return String [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     * @version [版本号, 2016年3月7日]
     * @author rain
     */
    public static String sendGetMethod(String url) {
        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
            HttpGet httpGet = new HttpGet(url);
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, "UTF-8");
        } catch (IOException e) {
            throw new RainhyException(e, "http GET error");
        } finally {
            IOUtils.closeQuietly(response);
            IOUtils.closeQuietly(client);
        }
    }
    
    /**
     * 
     * 发送 GET 请求
     *
     * @param url
     * @param header
     * @return
     * 
     * @return String [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     * @version [版本号, 2016年4月5日]
     * @author rain
     */
    public static String sendGetMethod(String url, Map<String, String> header) {
        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
            HttpGet httpGet = new HttpGet(url);
            if (MapUtils.isNotEmpty(header)) {
                for (Map.Entry<String, String> entry : header.entrySet()) {
                    httpGet.setHeader(entry.getKey(), entry.getValue());
                }
            }
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, "UTF-8");
        } catch (IOException e) {
            throw new RainhyException(e, "http GET error");
        } finally {
            IOUtils.closeQuietly(response);
            IOUtils.closeQuietly(client);
        }
    }
    
    /**
     * 
     * 通过 http get 方法获取 JsonObject 数据<br />
     * 如果调用失败,会抛出异常<br />
     * 如果返回的 json 数据中有"errcode",则会抛出异常
     *
     * @param url
     * 
     * @return JsonObject [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     * @version [版本号, 2015年10月27日]
     * @author rain
     */
    public static JsonObject sendGetMethodAndReturnJsonObjectByWeChat(String url) {
        JsonParser jsonParser = new JsonParser();
        
        String responseContent = sendGetMethod(url);
        JsonObject json = jsonParser.parse(responseContent).getAsJsonObject();
        JsonElement errcode = json.get("errcode");
        if (errcode != null && errcode.getAsInt() != 0) {
            throw new RainhyException("geturl异常   ----   " + url + "  ---   " + json.toString());
        }
        return json;
    }
    
    /**
     * 
     * post 协议请求
     *
     * @param url 请求地址
     * @param post 请求的数据
     * 
     * @return String [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     * @version [版本号, 2016年3月7日]
     * @author rain
     */
    public static String sendPostMethod(String url, String post, Map<String, String> header) {
        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new StringEntity(post, "UTF-8"));
            if (MapUtils.isNotEmpty(header)) {
                for (Map.Entry<String, String> entry : header.entrySet()) {
                    httpPost.setHeader(entry.getKey(), entry.getValue());
                }
            }
            response = client.execute(httpPost);
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, "UTF-8");
        } catch (IOException e) {
            throw new RainhyException(e, "http GET error");
        } finally {
            IOUtils.closeQuietly(response);
            IOUtils.closeQuietly(client);
        }
    }
    
    /**
     * 
     * 通过 http post 方法获取 JsonObject 数据
     *
     * @param url
     * @param post数据
     * 
     * @return JsonObject [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     * @version [版本号, 2015年10月27日]
     * @author rain
     */
    public static JsonObject sendPostMethodAndReturnJsonObjectByWeChat(String url, String post, Map<String, String> header) {
        JsonParser jsonParser = new JsonParser();
        String responseContent = sendPostMethod(url, post, header);
        JsonObject json = jsonParser.parse(responseContent).getAsJsonObject();
        if (json.get("errcode") != null) {
            throw new RainhyException("posturl异常   ----   " + url + "  ---   " + json.toString());
        }
        return json;
    }
}
